package test.reporters;

import java.io.Serializable;

import org.apache.jmeter.engine.util.NoThreadClone;
import org.apache.jmeter.reporters.AbstractListenerElement;
import org.apache.jmeter.samplers.Clearable;
import org.apache.jmeter.samplers.Remoteable;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.TestStateListener;
import org.apache.jmeter.visualizers.gui.AbstractVisualizer;



/**
 * 一个监听器示例，参考url：http://blog.csdn.net/xreztento/article/details/52576075
 * 
 * @author Administrator
 *监听器需要实现继承 abstractlistenerelement 类，可能实现某一个接口也行
 *SampleListener  取样器监听器   允许对取样器进行中发生的事件进行通知。如当抽样开始时，抽样进行中，抽样停止时
 * 多线程下可能需要继承类ResultCollector 
 */
public class MySampleResultCollector extends AbstractListenerElement implements 
SampleListener , Clearable,Serializable,TestStateListener,Remoteable,NoThreadClone{
		
	/**
	 * 用于判断sample是否成功，以便于过滤失败的sample
	 * @param success
	 * @return
	 */
	public boolean isSampleWanted(boolean success){
		if(success){
			return true;
		}
		return false;
	}
	
	
	@Override
	public void testStarted() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void testStarted(String host) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void testEnded() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void testEnded(String host) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sampleOccurred(SampleEvent e) {
		SampleResult result = e.getResult();
		if (isSampleWanted(result.isSuccessful())) {
			long during = result.getEndTime() - result.getStartTime();
			System.err.println(result.getSampleLabel()+":"+during+"ms");
		}
	}

	@Override
	public void sampleStarted(SampleEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sampleStopped(SampleEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	
}
